package beavers.studio.golfberry.fields;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import beavers.studio.golfberry.screens.GolfScreen;

public class GolfTabField extends Field {	
	public GolfTabField(int index) {
		//Super
		super();
		
		//Save
		m_Index = index;
		
		//Load images
		m_Logo		= Bitmap.getBitmapResource("header/logo.png");
		m_Cursor	= Bitmap.getBitmapResource("header/cursor.png");
		
		//Create icons
		m_Icons	= new Bitmap[GolfScreen.ID.values().length];
		for (int i = 0; i < GolfScreen.ID.values().length; i++) {
			m_Icons[i] = GolfScreen.ID.values()[i].getIcon(); 
		}
	}
	
	public int getPreferredWidth() {
		//Calculate width
		int Width = m_Logo.getWidth();
		for (int i = 0; i < m_Icons.length; i++) Width += m_Icons[i].getWidth();
		
		//Return it
		return Width;
	}
	
	public int getPreferredHeight() {
		//Calculate height
		int Height = m_Logo.getHeight();
		Height += SEPARATOR_HEIGHT;
		Height += getFont().getHeight();
		
		//Return
		return Height;
	}
	
	public boolean isFocusable() {
		return true;
	}
	
	public void next() {
		//Increment index
		m_Index++;
		if (m_Index >= m_Icons.length) m_Index = 0;
	}
	
	public void previous() {
		//Decrement index
		m_Index--;
		if (m_Index < 0) m_Index = m_Icons.length - 1;
	}
	
	public GolfScreen.ID getScreenID() {
		//Return ID
		return GolfScreen.ID.values()[m_Index];
	}
	
	protected void layout(int width, int height) {
		//Calculate width and height
		int Width 	= width;
		int Height	= getPreferredHeight();
		
		//Set size
		setExtent(Width, Height);
	}

	protected void paint(Graphics graphics) {
		//Draw background
		graphics.setColor(BACKGROUND_COLOR);
		graphics.fillRect(0, 0, getWidth(), getHeight());
		
		//Draw separator
		graphics.setColor(SEPARATOR_COLOR);
		graphics.fillRect(0, m_Logo.getHeight(), getWidth(), SEPARATOR_HEIGHT);
		
		//Draw logo
		graphics.drawBitmap(new XYRect(0, 0, m_Logo.getWidth(), m_Logo.getHeight()), m_Logo, 0, 0);		
		
		//Draw icons
		for (int i = 0; i < m_Icons.length; i++)
			graphics.drawBitmap(new XYRect(getWidth() - ((m_Icons.length - i) * ICON_WIDTH), 3, m_Icons[i].getWidth(), m_Icons[i].getHeight()), m_Icons[i], 0, 0);
		
		//Calculate
		String Label	= GolfScreen.ID.values()[m_Index].getLabel();
		int FocusX 		= getWidth() - ((m_Icons.length - m_Index) * ICON_WIDTH);
		int LabelWidth 	= getFont().getAdvance(Label);
		
		//Draw label
		graphics.setColor(LABEL_COLOR);
		graphics.fillRect(FocusX + ((ICON_WIDTH - LabelWidth) / 2), m_Logo.getHeight() + SEPARATOR_HEIGHT, LabelWidth, getFont().getHeight());
		graphics.setColor(LABEL_TEXT_COLOR);
		graphics.drawText(Label, FocusX + ((ICON_WIDTH - LabelWidth) / 2), m_Logo.getHeight() + SEPARATOR_HEIGHT, Graphics.LEFT);
		
		//Draw cursor
		graphics.drawBitmap(new XYRect(FocusX, 3, m_Cursor.getWidth(), m_Cursor.getHeight()), m_Cursor, 0, 0);
	}
	
	//Constants
	protected final int ICON_WIDTH			= 32;
	protected final int LABEL_COLOR			= 0x000000;
	protected final int LABEL_TEXT_COLOR	= 0xFFFFFF;
	protected final int SEPARATOR_HEIGHT 	= 2;
	protected final int SEPARATOR_COLOR 	= 0x000000;
	protected final int BACKGROUND_COLOR 	= 0xEE0000;
	
	//Data
	protected int 		m_Index;
	protected Bitmap	m_Logo;
	protected Bitmap	m_Cursor;
	protected String[]	m_Labels;
	protected Bitmap[]	m_Icons;
}
